/**
 * 
 */
package com.dchh.android.suverynew.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.dchh.android.suverynew.R;
import com.dhcc.android.base.common.bean.BasicProperties;
import com.dhcc.android.base.common.bean.File;
import com.dhcc.android.base.db.DBManagerUtil;
import com.dhcc.android.base.utils.CommenUtil;
import com.dhcc.android.base.utils.StringUtil;

import java.io.InputStream;

/**
 * 表单的新增页面
 * 
 * @author pengling
 * @date 2015-5-29
 */
public class AddFormActivity extends DemandMainActivity {
	private View view;
	private BasicProperties bp;
	private String path;
	private DBManagerUtil dBManagerUtil;
	private File file;
	private String DKID;
	private String rowid;
	private String surveyDKIDPath;
	private Object rowValue = null;
	private String rowKey;
	String title = "";

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.util_layout_carrier);
	}

	@Override
	public void initView() {
		rowKey = "rowid";
		file = (File) getIntent().getSerializableExtra("file");
		bp = (BasicProperties) getIntent().getSerializableExtra("bp");
		rowid = getIntent().getStringExtra("rowid");
		DKID = getIntent().getStringExtra(file.getPfeild());
		path = getIntent().getStringExtra("path");
		if(bp!=null){
			title = bp.getTitle();
		}else{
			title = file.getName();
		}

		if (StringUtil.isNotEmpty(rowid)) {
			title = title.replace("新增", "") + "详情";
			rowValue = Integer.parseInt(rowid);
		}else if(!title.contains("增")){
			title="新增"+title;
		}
		surveyDKIDPath = getIntent().getStringExtra("surveyDKIDPath");
		dBManagerUtil = DBManagerUtil.getInstance(path, this);
		setTitle(title);
		addMap.clear();
		initPhotoUtil(surveyDKIDPath);
		LinearLayout save_layout = (LinearLayout) view
				.findViewById(R.id.save_layout);
		save_layout.setVisibility(View.VISIBLE);
		save_layout.setOnClickListener(itemOnClick);
		LinearLayout carrier_layout = (LinearLayout) view
				.findViewById(R.id.carrier_layout);// 子页面载体，解决有多个页面的情况
		carrier_layout.setPadding(0, 0, 0, 80);
		InputStream input = CommenUtil.initStreamData(file.getProperties(), path, this);
		initFormView(carrier_layout, input, view, file, rowValue, rowKey,
				dBManagerUtil, DKID,title);
		setNavigationOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				goBack();
			}
		});
	}

	/**
	 * 逻辑监听
	 */
	public OnClickListener itemOnClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			int id = v.getId();
			if(id== R.id.save_layout){
				if (StringUtil.isFastDoubleClick(500)) {
					return;
				}
				int i = saveData(file, dBManagerUtil, rowKey, rowid, true);
				switch (i) {
				case 0:// 跳出监听
					return;
				case 1:// 成功
					CommenUtil.toast("保存成功！", AddFormActivity.this);
					// CommenUtil.createFile(surveyDKIDPath);
					// CommenUtil.copyFile(path + "DATA.db", surveyDKIDPath);
					Intent startIntent = new Intent(AddFormActivity.this,
							DemandMainActivity.class);
					startIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					// startIntent.addFlags(Intent.
					// FLAG_ACTIVITY_REORDER_TO_FRONT);
					startActivity(startIntent);
					finish();
					overridePendingTransition(R.anim.push_left_in,
							R.anim.push_right_out);
					break;
				case 2:// 失败
					CommenUtil.toast("保存失败！", AddFormActivity.this);
					break;
				default:
					break;
				}
			}
		}
	};

	private void goBack() {
		if (!isClickSave && !addMap.isEmpty() && !oldDataMap.equals(addMap)) {
			isAlertDown(AddFormActivity.this, "是否撤销此次编辑？", null, "确定", "取消", 0,
					null, null);
		} else {
			finish();
			overridePendingTransition(R.anim.push_left_in,
					R.anim.push_right_out);
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			goBack();
		}
		return true;
	}
}
